package com.sright.bcard.content.biz.service;

import com.sright.bcard.common.service.share.dto.content.CardUserCompanyInfoDTO;
import com.sright.bcard.common.service.share.dto.content.ContentUserInfoDTO;
import com.sright.bcard.common.service.share.dto.content.UserInfoCheckResultDTO;
import com.sright.bcard.common.service.share.dto.page.BetweenDateParamRequest;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.dal.dao.CardSettingDAO;
import com.sright.bcard.content.dal.dao.CardUserCompanyInfoDAO;
import com.sright.bcard.content.dal.dao.CardUserInfoDAO;
import com.sright.bcard.content.dal.dao.UserInfoCheckResultDAO;
import com.sright.bcard.content.dal.entity.CardSettingDO;
import com.sright.bcard.content.dal.entity.CardUserCompanyInfoDO;
import com.sright.bcard.content.dal.entity.CardUserInfoDO;
import com.sright.bcard.common.service.share.dto.content.CardUserInfoDTO;
import com.sright.bcard.content.dal.entity.UserInfoCheckResultDO;
import com.sright.bcard.content.share.service.CardUserInfoService;
import com.sright.bcard.enterprise.dal.dao.AddressDAO;
import com.sright.bcard.enterprise.dal.dao.CompanyDAO;
import com.sright.bcard.enterprise.dal.entity.AddressDO;
import com.sright.bcard.enterprise.dal.entity.CompanyDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Author: lumf
 * Date: 2020/8/20 1:36 PM
 */
@Service
@Slf4j
public class CardUserInfoServiceImpl implements CardUserInfoService {
    @Autowired
    CardUserInfoDAO cardUserInfoDAO;

    @Autowired
    private CardUserCompanyInfoDAO cardUserCompanyInfoDAO;

    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private AddressDAO addressDAO;

    @Autowired
    private UserInfoCheckResultDAO userInfoCheckResultDAO;

    @Autowired
    private CardSettingDAO cardSettingDAO;

    @Override
    public Boolean updateUserInfo(CardUserInfoDTO userInfoDTO) {
        CardUserInfoDO cardUserInfoDO = new CardUserInfoDO();
        DataConvertUtils.convertDTO2DO(userInfoDTO, cardUserInfoDO);
        return cardUserInfoDAO.updateSelective(cardUserInfoDO);
    }

    @Override
    public Pager<ContentUserInfoDTO> getCardUserInfoList(Integer checkStatus, BetweenDateParamRequest betweenDateParamRequest, PageParamRequest pageParamRequest) {
        Integer count = cardUserInfoDAO.countOfCardUserInfo(checkStatus, betweenDateParamRequest);
        List<CardUserInfoDO> cardUserInfoDOS = cardUserInfoDAO.selectOfCardUserInfo(checkStatus, betweenDateParamRequest, pageParamRequest);
        // 查询公司名片信息
        List<CardUserCompanyInfoDTO> cardUserCompanyInfoDTOS = new ArrayList<>();
        List<ContentUserInfoDTO> cardUserInfoDTOS = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(cardUserInfoDOS)) {
            DataConvertUtils.convertDOList2DTOList(cardUserInfoDOS, cardUserInfoDTOS, ContentUserInfoDTO.class);
            List<Long> cardUserInfoIds = cardUserInfoDOS.stream().map(CardUserInfoDO::getId).collect(Collectors.toList());
            List<CardUserCompanyInfoDO> cardUserCompanyInfoDOS = cardUserCompanyInfoDAO.selectByCardUserInfoIds(cardUserInfoIds);
            if(CollectionUtils.isNotEmpty(cardUserCompanyInfoDOS)) {
                DataConvertUtils.convertDOList2DTOList(cardUserCompanyInfoDOS, cardUserCompanyInfoDTOS, CardUserCompanyInfoDTO.class);
            }
            List<Long> companyIds = cardUserCompanyInfoDOS.stream().map
                    (CardUserCompanyInfoDO::getCompanyId).collect(Collectors.toList());
            List<CompanyDO> companyDOS = companyDAO.selectByCompanyIdList(companyIds);
            if(CollectionUtils.isNotEmpty(companyDOS)) {
                Map<Long, CompanyDO> longCompanyDOMap = companyDOS.stream().collect
                        (Collectors.toMap(CompanyDO::getId, a -> a, (k1, k2) -> k1));
                cardUserCompanyInfoDTOS.stream().forEach(cardUserCompanyInfoDTO -> {
                    CompanyDO companyDO = longCompanyDOMap.get(cardUserCompanyInfoDTO.getCompanyId());
                    if(null != companyDO) {
                        cardUserCompanyInfoDTO.setName(companyDO.getName());
                        cardUserCompanyInfoDTO.setNameEn(companyDO.getNameEn());
                    }
                });
            }
            List<Long> addressIds = cardUserInfoDTOS.stream().map(ContentUserInfoDTO::getAddressId).collect(Collectors.toList());
            List<AddressDO> addressDOS = addressDAO.selectByAddressIds(addressIds);
            Map<Long, AddressDO> longAddressDOMap = addressDOS.stream().collect
                    (Collectors.toMap(AddressDO::getId, a -> a, (k1, k2) -> k1));
            cardUserInfoDTOS.stream().forEach(cardUserInfoDTO -> {
                AddressDO addressDO = longAddressDOMap.get(cardUserInfoDTO.getAddressId());
                if(null != addressDO) {
                    cardUserInfoDTO.setAddressZh(addressDO.getAddressZh());
                }
            });
        }
        Map<Long, List<CardUserCompanyInfoDTO>> longListMap = cardUserCompanyInfoDTOS.stream().
                collect(Collectors.groupingBy(CardUserCompanyInfoDTO::getCardUserInfoId));
        cardUserInfoDTOS.stream().forEach(cardUserInfoDTO -> {
            cardUserInfoDTO.setCardUserCompanyInfoDTOS(longListMap.get(cardUserInfoDTO.getId()));

            CardSettingDO cardSettingDO = cardSettingDAO.selectByUserId(cardUserInfoDTO.getUserId());
            if (cardSettingDO == null) {
                cardUserInfoDTO.setForwardTitle("");
                cardUserInfoDTO.setVisitorTitle("");
            } else {
                cardUserInfoDTO.setForwardTitle(cardSettingDO.getForwardTitle());
                cardUserInfoDTO.setVisitorTitle(cardSettingDO.getVisitorTitle());
            }

            UserInfoCheckResultDO userInfoCheckResultDO = userInfoCheckResultDAO.selectByUserId(cardUserInfoDTO.getUserId());
            if (userInfoCheckResultDO == null) {
                userInfoCheckResultDO = new UserInfoCheckResultDO();
                userInfoCheckResultDO.setName(0);
                userInfoCheckResultDO.setCompany(0);
                userInfoCheckResultDO.setOtherCompany(0);
                userInfoCheckResultDO.setDepartment(0);
                userInfoCheckResultDO.setOtherDepartment(0);
                userInfoCheckResultDO.setJob(0);
                userInfoCheckResultDO.setOtherJob(0);
                userInfoCheckResultDO.setAddress(0);
                userInfoCheckResultDO.setForwardTitle(0);
                userInfoCheckResultDO.setVisitorTitle(0);
            }

            UserInfoCheckResultDTO userInfoCheckResultDTO = DataConvertUtils.convertDO2DTO(userInfoCheckResultDO,UserInfoCheckResultDTO.class);

            cardUserInfoDTO.setCheckResult(userInfoCheckResultDTO);
        });
        return new Pager<>(pageParamRequest.getPageIndex(), pageParamRequest.getPageSize(), count, cardUserInfoDTOS);
    }
}
